package cn.iocoder.yudao.module.train.controller.wechat.classsetting;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.train.controller.admin.classsetting.vo.ClassSettingPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.classsetting.vo.ClassSettingRespVO;
import cn.iocoder.yudao.module.train.controller.admin.classsetting.vo.ClassSettingSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.CourseClassesPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.CourseCommoditySaveReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.classsetting.ClassSettingDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.ClassSettingDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import cn.iocoder.yudao.module.train.service.classsetting.ClassSettingService;
import cn.iocoder.yudao.module.train.service.courseclasses.CourseClassesService;
import cn.iocoder.yudao.module.train.service.coursestudentregistform.CourseStudentRegistFormService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.GET;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序接口 - 排课")
@RestController
@RequestMapping("/wechat-api/class-setting")
@Validated
public class WechatClassSettingController {

    @Resource
    private ClassSettingService classSettingService;

    @Resource
    private CourseClassesService courseClassesService;

    @Resource
    private CourseStudentRegistFormService courseStudentRegistFormService;

    @GetMapping("/list")
    @Operation(summary = "获得排课列表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<ClassSettingRespVO>> getClassSettingList(Long commodityId) {

        if (commodityId == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }

        List<ClassSettingDTO> classSettingDOS = new ArrayList<>();
        ClassSettingPageReqVO classSettingPageReqVO = new ClassSettingPageReqVO();
        List<CourseClassesCommodityDTO> courseClassesList = courseClassesService.getCourseClassesList(new CourseClassesPageReqVO().setCommodityId(commodityId));

        courseClassesList.forEach(courseClassesDO -> {
            classSettingPageReqVO.setClassesId(courseClassesDO.getId());
            classSettingDOS.addAll(classSettingService.getClassSettingListDetail(classSettingPageReqVO));

        });
        classSettingDOS.forEach(classSettingDO -> {
            classSettingDO.setClassName(classSettingDO.getCommodityName()+"-"+classSettingDO.getClassNumber()+"班");
        });
        return success(BeanUtils.toBean(classSettingDOS, ClassSettingRespVO.class));
    }


    @GetMapping("/listByStudentId")
    @Operation(summary = "获得学生排课表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<ClassSettingRespVO>> getClassSettingListByStudentId(Long studentId) {

        if (studentId == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }

        List<ClassSettingDTO> classSettingDOS = new ArrayList<>();
        ClassSettingPageReqVO classSettingPageReqVO = new ClassSettingPageReqVO();
        List<CourseStudentRegistFormDTO> courseStudentRegistFormListBystudentId = courseStudentRegistFormService.getCourseStudentRegistFormListBystudentId(studentId);
        courseStudentRegistFormListBystudentId.forEach(courseStudentRegistForm -> {
            classSettingPageReqVO.setClassesId(courseStudentRegistForm.getClassesId());
            classSettingDOS.addAll(classSettingService.getClassSettingListDetail(classSettingPageReqVO));
        });
        classSettingDOS.forEach(classSettingDO -> {
            classSettingDO.setClassName(classSettingDO.getCommodityName()+"-"+classSettingDO.getClassNumber()+"班");
        });
        return success(BeanUtils.toBean(classSettingDOS, ClassSettingRespVO.class));
    }

}
